﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using Yipusoft.DepartmentRelegate;

public partial class BasicBase_DepartmentRelegate_RelegateReport : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {
        BindCityClass();
        ListPageContral1.SetFirstPage();
        Bind(0);
    }



    private void BindCityClass()
    {
        ddlCity.DataSource = CodeItem.GetList(" parentId='SS030000' order by itemID");
        ddlCity.DataBind();
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        Bind(PageIndex);
    }



    void Bind(int pageIndex)
    {
        RelegateDataContext db = new RelegateDataContext();
        List<DepartmentRelegate> list = new List<DepartmentRelegate>();
        list = (from o in db.DepartmentRelegate where o.RegionId.Substring(0, 6) == ddlCity.SelectedValue.Substring(0, 6) orderby o.DepartmentId ascending select o).ToList();

        int count = list.Count;
        ListPageContral1.ItemCount = count;
        GV.DataSource = list.Skip(ListPageContral1.PageSize * pageIndex).Take(ListPageContral1.PageSize);
        GV.DataBind();
    }

    List<DepartmentRelegate> getDepartmentRelegate(List<DepartmentRelegate> list, string departmentId)
    {
        RelegateDataContext db = new RelegateDataContext();
        DepartmentRelegate info = db.DepartmentRelegate.Where(o => o.DepartmentId == departmentId).FirstOrDefault();
        list.Add(info);
        List<DepartmentRelegate> chailds = db.DepartmentRelegate.Where(o => o.parentId == departmentId).ToList();
        if (chailds.Count > 0)
        {
            foreach (DepartmentRelegate gelegate in chailds)
            {
                list.AddRange(getDepartmentRelegate(list, gelegate.DepartmentId));
            }
        }
        return list;
    }
    protected void gvChemical_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblID = e.Row.FindControl("lblID") as Label;
            lblID.Text = Convert.ToString(GV.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            DepartmentRelegate relegate = e.Row.DataItem as DepartmentRelegate;
            Label lblDepartmentName = e.Row.FindControl("lblDepartmentName") as Label;
            lblDepartmentName.Text = Department.GetDepartment(relegate.DepartmentId).FullName;
            CheckBox cbXZ = e.Row.FindControl("cbXZ") as CheckBox;
            cbXZ.Checked = Convert.ToBoolean(relegate.IsCitylevel);
            cbXZ.Enabled = false;
            Label lblParentName = e.Row.FindControl("lblParentName") as Label;
            lblParentName.Text = Department.GetDepartment(relegate.parentId).FullName;
        }
    }
    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        Bind(0);
    }
}
